package com.lmmob.autotest.uitest.controls;

import org.openqa.selenium.WebDriver;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class CompositeControl extends Control {
    protected List<Control> children;

    private Map<String, Control> name2controlMap = new HashMap<String, Control>();

    public CompositeControl(WebDriver webDriver) {
        super(webDriver);
        children = new ArrayList<Control>();
    }

    public void addChild(Control control) {
        this.children.add(control);
        name2controlMap.put(control.getName(), control);
        control.setParent(this);
    }

    public Control name2Control(String name) {
        return this.name2controlMap.get(name);
    }

    public Control getChild(Integer index) {
        return this.children.get(index);
    }

    @Override
    public void show() {
        for (Control control : children) {
            control.show();
        }
    }
}
